/**
* 整体字母排序
* @param {Array} arr 原数组
* @param {string} key 需要根据某个键名排序
* @return 排序后的新数组
*/
export const sortAlphabet = (arr, key) => {
  let newArr = []
  if (key) {
    newArr = arr.sort((a, b) => a[key].localeCompare(b[key]))
  } else {
    newArr = arr.sort()
  }
  return newArr
}

export const getCurrentList = (addressList) => {
  const alphabetList = []
  for (let i = 0; i < 26; i++) {
    alphabetList.push({ code: String.fromCharCode(65 + i), data: [] })
  }
  addressList.forEach(async item => {
    if (!item.user_id) {
      return
    }
    const newItem = {}
    newItem.user_id = item.user_id
    newItem.user_img = item.user_img
    newItem.user_name = item.user_name
    newItem.remarks = item.remarks
    newItem.user_phone = item.user_phone
    newItem.pingyin = item.remarks_pinyin ? item.remarks_pinyin : item.user_pinyin
    alphabetList.forEach(it => {
      if (newItem.pingyin[0].toUpperCase() === it.code) {
        it.data.push(newItem)
      }
    })
  })
  const currentList = alphabetList.filter(item => item.data.length)
  currentList.forEach(item => {
    item.data = sortAlphabet(item.data, 'pingyin')
  })
  return currentList
}
